import Vue from 'vue'
import Router from 'vue-router'
import {appChildRoutes} from 'app/config.js'

Vue.use(Router)


const router =  new Router({
  routes: appChildRoutes
});


// 登录授权
router.beforeEach(function(to, from, next){
  //todo: 登录的meta字段
  let needLogIn = to.matched.some(function(record){return record.meta.needLogIn});
  if (needLogIn) {

    if (router.app.$store.state.isLogIn) {
      next()
    } else {
      // fixme: 跳到登录页面
      next({
        name: 'LogIn',
        query: { next: to.fullPath }
      })
    }
  } else {
    next();
  }
});





// 加载状态启动
router.beforeEach(function(to, from, next){
  //登录的meta字段
  let needLood = to.matched.some(function(record){return record.meta.needLood});
  if (needLood) {
  //  todo: 显示加载状态

  }
  next();
});

// 加载状态关闭
router.afterEach(function (to, from) {
  // 登录的meta字段
  let needLood = to.matched.some(function(record){return record.meta.needLood});
  if (needLood) {
    //  todo: 关闭加载状态

  }
});




export default router
